package com.woniu.job.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.job.entity.User;
import com.woniu.job.entity.vo.ResultVO;
import com.woniu.job.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sunnylausong
 * 管理用户
 * 2019/12/27
 * 15:19
 */
@RestController("adminUserController")
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    IUserService userService;

    /**
     * 获取用户列表分页
     * @param p page
     * @return
     */
    @GetMapping("/list/{p}")
    public ResultVO<Page<User>> getList(@PathVariable int p)
    {
        p = p < 1 ? 1 : p;
        // pageSize=10
        Page<User> list = userService.page(new Page<>(p, 10));
        // 去掉密码
        list.getRecords().forEach(t->{
            t.password(null);
        });
        return new ResultVO<>(list);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/update/{id}/{forbidden}")
    public ResultVO delete(@PathVariable String id, @PathVariable Boolean forbidden)
    {
        String status = null;
        if (forbidden) {
            status = "2";
        } else {
            status = "1";
        }
        userService.updateById(new User().id(id).status(status));
        return ResultVO.SUC.message("修改成功！");
    }
}
